package com.dai.zhenfujin.my.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dai.zhenfujin.Constants.Constants;
import com.dai.zhenfujin.Constants.EventConstants;
import com.dai.zhenfujin.R;
import com.dai.zhenfujin.base.BaseActivity;
import com.dai.zhenfujin.bean.MessageEvent;
import com.dai.zhenfujin.bean.ProListBean;
import com.dai.zhenfujin.http.CallBack;
import com.dai.zhenfujin.http.HttpClient;
import com.dai.zhenfujin.my.bean.MyBean;
import com.dai.zhenfujin.visitingcard.app.adapter.PersonalDetailsListviewAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的发布
 */
public class MyActivity extends BaseActivity {
    private TextView tvName,tvCard,tvProduct;
    private ImageView imageView;
    private ListView listView;
    private FrameLayout fl_alum;

    private List<MyBean> beans;
    private int page = 1;
    private int number = 10;
    private PersonalDetailsListviewAdapter adapter;
    private List<ProListBean.ProListBean2> proListBean = new ArrayList<>();
    private SmartRefreshLayout refreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        EventBus.getDefault().register(this);
        initView();
        initListener();
        onRefresh();
        initRefresh();
    }

    private void initRefresh() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getMyProductList(true);
                        refreshLayout.finishRefresh();
                        refreshLayout.resetNoMoreData();
                    }
                },1500);
            }
        });

        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                getMyProductList(false);
            }
        });
    }

    private void onRefresh() {
        page = 1;
        getMyProductList(true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event != null && event.getMessageKey() != null) {
            if (event.getMessageKey().equals(EventConstants.UPDATA_PRODUCT_LIST)) {
                page = 1;
                getMyProductList(true);
            }
        }
    }


    private void initListener() {
        imageView.setOnClickListener(v -> finish());
    }

    private void initView() {
        imageView = findViewById(R.id.title_imageview);
        tvName = findViewById(R.id.title_textview);
        tvName.setText("我的主业和副业");
        listView = (ListView) findViewById(R.id.my_my_listview);
        fl_alum = (FrameLayout) findViewById(R.id.fl_alum);
        fl_alum.setVisibility(View.INVISIBLE);
        refreshLayout = findViewById(com.dai.zhenfujin.R.id.refreshLayout);

        adapter = new PersonalDetailsListviewAdapter(MyActivity.this, proListBean);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener((parent, view, position, id) -> {
           // Intent intent = new Intent(this, ParticularsPageActivity.class);
          //  intent.putExtra("pro_id", proListBean.get(position).getPro_id());
          //  intent.putExtra("type_id", proListBean.get(position).type_id);
           // startActivity(intent);
        });
        fl_alum.setOnClickListener(v -> finish());
    }

    private void getMyProductList(boolean isRefresh) {
        Map<String, Object> params = new HashMap<>();
        Map<String, String> params2 = new HashMap<>();
        params2.put("page", page + "");
        params2.put("number", number+ "");
        params.put("req_bd",  params2);
        HttpClient.post(this, Constants.MYPRODUCTLIST, params, new CallBack<ProListBean>() {
            @Override
            public void onSuccess(ProListBean resultBean) {
                if (isRefresh) {
                    proListBean.clear();
                    page = 2;
                } else {
                    page++;
                    refreshLayout.finishLoadmore();
                }
                if (resultBean.pro_list.size() > 0) {
                    proListBean.addAll(resultBean.pro_list);
                    adapter.notifyDataSetChanged();
                } else {
                    refreshLayout.finishLoadmoreWithNoMoreData();//将不会再次触发加载更多事件
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
